package org.iteam.xlz.manager.action.terminal;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.terminal.ProductTerminal;
import org.iteam.xlz.core.service.base.AreaService;
import org.iteam.xlz.core.service.product.ProductService;
import org.iteam.xlz.core.service.terminal.TerminalProductService;
import org.iteam.xlz.core.service.terminal.TerminalService;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/terminal/product/index.html"),
    @Result(name="EDIT",type="freemarker",location="/WEB-INF/content/terminal/product/edit.html")})
public class TerminalProductAction extends AbstractModuleAction<ProductTerminal> implements RestAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1528566094693060722L;
	private final String PATH="/terminal/product";
	@Override
	public ProductTerminal getModel() {
		if(null==model){
			model=new ProductTerminal();
		}
		return model;
	}

	@Override
	@Action(PATH+"/index")
	public String index() throws Exception {
		return INDEX;
	}

	@Override
	@Action(PATH+"/query")
	public String query() {
		map.put("total",TerminalProductService.getInstance().counts());
		map.put("rows",TerminalProductService.getInstance().query(page,rows));
		return MAP;
	}

	@Override
	public String show() {
		return null;
	}

	@Override
	@Action(PATH+"/update")
	public String update() {
		map.put("state",TerminalProductService.getInstance().update(model,amount,ShiroUtil.currentAccount()));
		return MAP;
	}

	@Override
	@Action(PATH+"/search")
	public String delete() {
		map.put("terminals",TerminalService.getInstance().queryTerminals(model.getPid(),model.getTid()));
		return MAP;
	}

	@Override
	@Action(PATH+"/{_id}/delete")
	public String destroy() {
		map.put("state",TerminalProductService.getInstance().down(_id,ShiroUtil.currentAccount()));
		return MAP;
	}

	@Override
	@Action(PATH+"/{_id}/edit")
	public String edit() throws Exception {
		super.object=TerminalProductService.getInstance().findOne(_id);
		map.put("products",ProductService.getInstance().getActiveProduct());
		AreaService.getInstance().buildAreaTree();
		map.put("areaTree",AreaService.getInstance().getList());
		return EDIT;
	}

	@Override
	@Action(PATH+"/new")
	public String editNew() throws Exception {
		map.put("products",ProductService.getInstance().getActiveProduct());
		AreaService.getInstance().buildAreaTree();
		map.put("areaTree",AreaService.getInstance().getList());
		return EDIT;
	}

	@Override
	public String create() {
		return null;
	}
	
	private int amount;
	public void setAmount(int amount) {
		this.amount = amount;
	}

}
